MkCode
Class representing a Code block.¶
Example: Default¶
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="n">a</span> <span class="o">=</span> <span class="mi">1</span> <span class="o">+</span> <span class="mi">2</span>
</span></code></pre></div>
Example: Syntax highlighting¶
<div class="language-js highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="kd">var</span><span class="w"> </span><span class="nx">z</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="nx">x</span><span class="w"> </span><span class="o">+</span><span class="w"> </span><span class="nx">y</span><span class="p">;</span>
</span></code></pre></div>
Example: Highlighting lines¶
<div class="language-python highlight"><pre><span></span><code><span id="__span-0-1"><a id="__codelineno-0-1" name="__codelineno-0-1" href="#__codelineno-0-1"></a><span class="hll"><span class="mi">1</span>
</span></span><span id="__span-0-2"><a id="__codelineno-0-2" name="__codelineno-0-2" href="#__codelineno-0-2"></a><span class="mi">2</span>
</span><span id="__span-0-3"><a id="__codelineno-0-3" name="__codelineno-0-3" href="#__codelineno-0-3"></a><span class="hll"><span class="mi">3</span>
</span></span><span id="__span-0-4"><a id="__codelineno-0-4" name="__codelineno-0-4" href="#__codelineno-0-4"></a><span class="mi">4</span>
</span></code></pre></div>
Example: Line numbers¶
<div class="language-python highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span></span><span class="normal"><a href="#__codelineno-0-10">10</a></span>
<span class="normal"><a href="#__codelineno-0-11">11</a></span>
<span class="normal"><a href="#__codelineno-0-12">12</a></span>
<span class="normal"><a href="#__codelineno-0-13">13</a></span></pre></div></td><td class="code"><div><pre><span></span><code><span id="__span-0-10"><a id="__codelineno-0-10" name="__codelineno-0-10"></a><span class="mi">1</span>
</span><span id="__span-0-11"><a id="__codelineno-0-11" name="__codelineno-0-11"></a><span class="mi">2</span>
</span><span id="__span-0-12"><a id="__codelineno-0-12" name="__codelineno-0-12"></a><span class="mi">3</span>
</span><span id="__span-0-13"><a id="__codelineno-0-13" name="__codelineno-0-13"></a><span class="mi">4</span>
</span></code></pre></div></td></tr></table></div>
Bases: MkContainer
__init__
¶
__init__(
content: str | MkNode | list = "",
*,
language: str = "python",
title: str = "",
linenums: int | None = None,
highlight_lines: list[int] | None = None,
fence_level: int | None = None,
**kwargs: Any
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str | MkNode | list
|
Content to show inside code box |
''
|
language
|
str
|
language for syntax highlighting |
'python'
|
title
|
str
|
Code block title |
''
|
linenums
|
int | None
|
If set, use as start linenumber |
None
|
highlight_lines
|
list[int] | None
|
Optionally highlight lines |
None
|
fence_level
|
int | None
|
Determines amount of ticks used for fence. If None, auto-determine based on nesting depth. |
None
|
kwargs
|
Any
|
Keyword arguments passed to parent |
{}
|
for_file
classmethod
¶
for_file(
path: str | PathLike[str],
*,
linenums: bool = True,
highlight_caller: bool = True,
title: str | None = None,
language: str | None = None,
**kwargs: Any
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
path
|
str | PathLike[str]
|
Path to the code file (also supports fsspec-protocol URLs) |
required |
linenums
|
bool
|
Whether to show line numbers |
True
|
highlight_caller
|
bool
|
Whether we want to try to highlight the line which called this method. |
True
|
title
|
str | None
|
title to use for the code box. If None is set, filename will be used. |
None
|
language
|
str | None
|
Syntax highlighting language. If None, try to infer from extension. |
None
|
kwargs
|
Any
|
Keyword arguments passed to MkCode ctor |
{}
|
for_object
classmethod
¶
for_object(
obj: HasCodeType,
*,
dedent: bool = True,
extract_body: bool = False,
title: str | None = None,
linenums: bool = True,
highlight_caller: bool = True,
**kwargs: Any
) -> Self
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
HasCodeType
|
Python object to show code from |
required |
dedent
|
bool
|
Whether to dedent the code |
True
|
extract_body
|
bool
|
if True, Function / Class signatures are stripped from the code |
False
|
title
|
str | None
|
Title to use for code block. If None, it will use the object path. |
None
|
linenums
|
bool
|
Whether to show line numbers |
True
|
highlight_caller
|
bool
|
Whether we want to try to highlight the line which called this method. |
True
|
kwargs
|
Any
|
Keyword arguments passed to MkCode ctor |
{}
|
Name | Children | Inherits |
---|---|---|
MkDiagram mknodes.basenodes.mkdiagram Class representing a mermaid diagram. |
||
MkTreeView mknodes.templatenodes.mktreeview Node to display tree structures. |
Name | Children | Inherits |
---|---|---|
MkContainer mknodes.basenodes.mkcontainer A node containing other MkNodes. |
graph TD
94721306031680["mkcode.MkCode"]
94721311697232["mkcontainer.MkContainer"]
94721308848336["mknode.MkNode"]
94721311766592["node.Node"]
140564252373184["builtins.object"]
94721311697232 --> 94721306031680
94721308848336 --> 94721311697232
94721311766592 --> 94721308848336
140564252373184 --> 94721311766592
[metadata]
icon = "mdi:code-json"
name = "MkCode"
[requirements.extension."pymdownx.superfences"]
[requirements.extension."pymdownx.highlight"]
anchor_linenums = true
line_spans="__span"
pygments_lang_class = true
[examples.title]
title = "Default"
description = "By default, python syntax highlighting is used."
jinja = """
{{ "a = 1 + 2" | MkCode }}
"""
[examples.syntax_highlight]
title = "Syntax highlighting"
jinja = """
{{ "var z = x + y;" | MkCode(language="js") }}
"""
[examples.hl_lines]
title = "Highlighting lines"
jinja = """
{{ "1\n2\n3\n4" | MkCode(highlight_lines=[1, 3]) }}
"""
[examples.line_numbers]
title = "Line numbers"
jinja = """
{{ "1\n2\n3\n4" | MkCode(linenums=10) }}
"""
[output.markdown]
template = """
{{ node.fence_boundary }} {{ node.fence_title }}
{{ node.text }}
{{ node.fence_boundary }}
"""
[output.rst]
template = """
.. sourcecode:: {{ node.language }}
{{ node.text | indent(first=True) }}
"""
mknodes.basenodes.mkcode.MkCode | |
---|---|
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 |
|